<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./css/bootstrap.min.css">
</head>

<body>
    <!-- 评论面板 -->
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3>发表评论</h3>
        </div>
        <form class="panel-body">
            <div>评论人：</div>
            <input type="text" name="username" class="form-control">
            <div>评论内容：</div>
            <textarea name="content" class="form-control"></textarea>
            <button type="submit" class="btn btn-primary">发布</button>
        </form>
    </div>
    <!-- 评论列表 -->
    <ul class="list-group" id="lie">
        <!-- <li class="list-group-item">
            <span class="badge" style="background-color: #f0ad4e;">评论时间：</span>
            <span class="badge" style="background-color: #5bc0de;">评论人：</span>
        </li> -->
    </ul>
</body>

</html>
<script src="./js/jquery.js"></script>
<script>
    $(function () {
        function getCmt() {
            $.ajax({
                type: 'get',
                url: 'http://www.liulongbin.top:3006/api/cmtlist',
                success: function (res) {
                    if (res.status != 200) {

                    } else {
                        const arr = [];
                        $.each(res.data, function (i, item) {
                            const str =
                                `
                        <li class="list-group-item">
                            ${item.content}
                        <span class="badge" style="background-color: #f0ad4e;">评论时间：${item.time}</span>
                        <span class="badge" style="background-color: #5bc0de;">评论人：${item.username}</span>
                        </li>
                        `;
                            arr.push(str);
                        })
                        $('#lie').empty().append(arr.join(''));
                    }
                }
            })
        } getCmt();
        // 添加评论
        $('form').on('submit', function (e) {
            e.preventDefault();
            var data1 = $(this).serialize();
            // 发送请求
            $.ajax({
                type: 'post',
                url: 'http://www.liulongbin.top:3006/api/addcmt',
                data: data1,
                success: function (res) {
                    if (res.status != 201) {
                        return alert('发布评论失败')
                    } else {
                        getCmt();
                        $('form')[0].reset();
                    }
                }
            })
        })
    })
</script>